var app = getApp();
const addCarUrl = "/ShopCart/joinCart";
// 立即购买接口地址
const buyImmediatelyUrl = '/Order/buyGoods';
const getAttributeUrl = '/user/getGoodsAttr';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isGetAttributing:false,//是否正在点击加入购物车按钮
    isLoading: 1,//是否显示加载中图标
    isClick:1,//是否可以点击
    is_bargain:0,//判断是否为砍价商品
    attributeImage: '',//选取商品尺码的盒子左上角图片
    shop_cart_is_show:0,//购物车是否显示
    user_level:0,//用户身份
    selectedAttributs: [], //已经选择过的商品属性列表
    ischooseSizeShow: 0, //控制选取尺寸盒子的显示
    runSelectTips: [], //
    car_shop_num:0,//购物车商品数量
    goodsAttibutes: [],//商品属性列表
    ischooseSizeShow: 0, //控制选取尺寸盒子的显示
    postage_money:0.00,//免运费还差的金额
    carousel_is_show: 1,//
    shop_id: 0,//专题ID
    goodslist: [],//商品列表
    page: 1,//初始化商品列表页码
    displayAuthShow: "none",
    goodsNum: 1,//用户选择商品的数量
    index: 1,//存储顶部轮播图的索引
    sourceUri: '',//静态资源地址
    filesSourceUri: '',//普通资源文件地址
    localSourceUri: '',//服务器目录存放地址
    carousel_adv_info: [],//轮播图列表
  },
  // 商品库存不足提示
  tipsOfGoodsNum: function () {
    wx.showToast({
      title: '该属性库存不足，请重新选择',
      duration: 2000,
      icon: "none"
    })
  },
  // 立即购买
  buyImmediately: function (e) {
    console.log(e);
    var _this = this;
    _this.setData({
      isClick:0
    })
    var isHasAddres = _this.checkAddress(0);
    var chooseSize = _this.isChooseSize();
    var goodsAttibutes = _this.data.goodsAttibutes;
    var responseData;
    var is_bargain = 1;
    if (e.currentTarget.dataset.is_bargain == 0) {
      is_bargain = 0;
    }
    if (goodsAttibutes.length == 0) {
      responseData = {
        version: app.setConfig.version,
        token: _this.data.token,
        type: app.setConfig.type,
        goods_id: _this.data.goodsId,
        goods_num: _this.data.goodsNum
      };
    } else {
      var attr_thumb = _this.data.attributeImage;
      var subStr = new RegExp('http://source.xingliaoapp.com/');
      attr_thumb = attr_thumb.replace(subStr, "");
      responseData = {
        is_bargain: is_bargain,
        version: app.setConfig.version,
        token: _this.data.token,
        type: app.setConfig.type,
        goods_id: _this.data.goodsId,

        attr_values: _this.data.selectAttributs,
        attr_thumb: attr_thumb,
        attr_pric: _this.data.selectAttributeprice,
        attr_vip_pric: _this.data.attr_vip_pric,
        attr_fans_pric: _this.data.attr_fans_pric,
        attr_broker_pric: _this.data.attr_broker_pric,
        goods_attr_id: _this.data.goods_attr_id,
        goods_num: _this.data.goodsNum
      };
    }
    console.log(isHasAddres, chooseSize);
    if (isHasAddres) {
      if (chooseSize) {
        _this.setData({
          isHasChoosed: 1
        })
        app.post(buyImmediatelyUrl, responseData, function (res) {
          if (res.code == 2000) {
            var is_jump_order = res.data.is_jump_order;
            if (is_jump_order == 0) {
              wx.navigateTo({
                url: '/pages/bargain/bargain?uid=0' + '&order_id=' + res.data.order_id,
              })
            } else if (is_jump_order == 1) {
              wx.navigateTo({
                url: '/pages/orderDetail/orderDetail?orderId=' + res.data.order_id,
              })
            }
          } else {
            wx.showToast({
              title: res.msg,
              duration: 2000,
              icon: "none"
            })
          }
        })
      }
    }
  },
  // 阻止点击事件
  preventEvent: function () {
  },
  // 跳转购物车页面
  goBuyCar:function(){
    wx.reLaunch({
      url: '/pages/buyCar/buyCar',
    })
  },
  // 免费领按钮
  goGoodsDetail: function (e) {
    var _this = this;
    var theGoodsId = e.currentTarget.dataset.goods_id;
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?goodsId=' + theGoodsId,
    })
  },
  // 关闭选取尺寸盒子
  closeChooseSize: function () {
    var _this = this;
    _this.setData({
      ischooseSizeShow: 0,
      selectTipsTitle: "请选择",
      selectAttributs: "",
      selectTips: "",
      selectedAttributs: []
    })
  },
  // 免费领取
  freeReceive: function (e) {
    var _this = this;
    var formId = e.detail.formId;
    app.postFormId(_this.data.token, formId);
    if (_this.data.goodsType == 0) {
      var data = {
        'goods_num': 1,
        'goods_id': _this.data.goodsId,
        'buyType': 1, //0 表示直接购买1表示分享下单
        'selectAttributs': "",
        'goods_price': _this.data.selectAttributeprice
      }
      wx.setStorageSync('cookie', data)
      _this.checkAddress(1);
    } else {
      _this.setData({
        ischooseSizeShow: 1,
        btnType: 1
      })
    }
  },
  /*直接购买商品*/
  goBuy: function (e) {
    var _this = this;
    var formId = e.detail.formId;
    app.postFormId(_this.data.token, formId);
    var goods_id = _this.data.goodsId;
    if (_this.data.goodsType > 0) {
      _this.setData({
        isHasChoosed: 1,
        ischooseSizeShow: 1,
        btnType: 0
      });
    } else {
      var buy_num = _this.data.goodsNum;
      var data = {
        'goods_num': 1,
        'goods_id': _this.data.goodsId,
        'buyType': 0, //0 表示直接购买1表示分享下单
        'selectAttributs': "",
        'goods_price': _this.data.selectAttributeprice
      }
      wx.setStorageSync('cookie', data)
      _this.checkAddress(0);
    }
  },
  //免费领表单提交
  buyFreeSubmitEvent: function (e) {
    var that = this;
    var formId = e.detail.formId;
    app.postFormId(that.data.token, formId);
    that.isChooseSize();
    var goods_id = that.data.goodsId;
    // 获取商品型号列表
    var goodsAttibutes = that.data.goodsAttibutes;
    if (goodsAttibutes.length == 0) {
      //获取商品类型 
      var get_type = that.data.get_type;
      if (get_type == 0) {
        // 一口价 平台订单
        wx.navigateTo({
          url: '/pages/orderDetail/orderDetail',
        })
      } else if (get_type == 1) {

      } else if (get_type == 2) {
        //助力
        var help_num = that.data.help_num;
        if (help_num == 0) {
          wx.navigateTo({
            url: '/pages/orderDetail/orderDetail',
          })
        } else {
          if (that.data.helpBoxShow == 1) {
            that.setData({
              helpBoxShow: 0
            });
          } else {
            that.setData({
              helpBoxShow: 1
            });
          }
        }

      }
    } else {
      that.setData({
        ischooseSizeShow: 1
      })
    }
  },
  /*更新选择的属性*/
  uploadSelectAttributes: function () {
    var _this = this;
    var goodsAttibutes = _this.data.goodsAttibutes;
    var selectAttrips = "";
    goodsAttibutes.forEach((attribute, index) => {
      attribute.attr_ext.forEach((v, i) => {
        if (v.is_selected == 1) {
          selectAttrips = selectAttrips + " " + v.attr_value
        }
      });
    });
    _this.setData({
      selectAttributs: selectAttrips
    });
    if (!_this.data.selectTips) {
      _this.setData({
        selectTips: selectAttrips,
        selectTipsTitle: '已选择'
      })
    }
  },
  // 获取商品数量
  getGoodsNum: function (e) {
    var _this = this;
    var theGoodsNum = Number(e.detail.value);
    var total_num = Number(_this.data.total_num);
    if (theGoodsNum > total_num) {
      wx.showToast({
        title: '没有更多商品了哦~',
        icon: 'error',
        duration: 2000
      });
      _this.setData({
        goodsNum: total_num
      })
      return false;
    } else if (theGoodsNum < 1) {
      wx.showToast({
        title: '不能再少了哦~',
        icon: 'error',
        duration: 2000
      });
    } else {
      _this.setData({
        goodsNum: theGoodsNum
      })
    }

  },
  // 增加商品数量
  plus: function () {
    var _this = this;
    var theGoodsNum = _this.data.goodsNum;
    theGoodsNum++;
    var total_num = _this.data.total_num;
    var is_bargain = _this.data.is_bargain;
    if (is_bargain == 1) {
      wx.showToast({
        title: '砍价商品限购1份',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (theGoodsNum > total_num) {
      wx.showToast({
        title: '没有更多商品了哦~',
        icon: 'none',
        duration: 2000
      });
      return false;
    } else {
      _this.setData({
        goodsNum: theGoodsNum
      })
    }

  },
  // 减少商品数量
  reduce: function () {
    var _this = this;
    var theGoodsNum = _this.data.goodsNum;
    var is_bargain = _this.data.is_bargain;
    if (is_bargain == 1) {
      wx.showToast({
        title: '砍价商品限购1份',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (theGoodsNum > 1) {
      theGoodsNum = theGoodsNum - 1;
    } else {
      wx.showToast({
        title: '不能再少了哦~',
        icon: 'error',
        duration: 2000
      });
    }
    _this.setData({
      goodsNum: theGoodsNum
    })
  },
  // 获取商品属性
  getAttribute:function(e){
    var _this = this;
    console.log(e.currentTarget.dataset.goods_type);
    _this.setData({
      isGetAttributing: true,
      goodsId:e.currentTarget.dataset.goods_id,
      goods_type: e.currentTarget.dataset.goods_type
    })
    if (e.currentTarget.dataset.goods_type==0){
      _this.setData({
        isGetAttributing:false,
        goodsAttibutes:[],
        goodsNum:1
      })
      _this.addCar();
      return false;
    }else{
    var requestData = {
      version: app.setConfig.version,
      token: _this.data.token,
      type: app.setConfig.type,
      goods_id: e.currentTarget.dataset.goods_id
    };
    app.post(getAttributeUrl, requestData, function (res) {
    if (res.code != 2000) {
      _this.setData({
        isGetAttributing: false
      })
      wx.showToast({
        title: res.msg,
        icon: 'none',
        duration: 2000
      })
    }else{
      _this.setData({
        isGetAttributing: false,
        ischooseSizeShow:1,
        attributeImage: e.currentTarget.dataset.attribute_image,
        is_show_buy_btn: e.currentTarget.dataset.is_show_buy_btn,
        is_bargain: e.currentTarget.dataset.is_bargain,
        is_open_shop: e.currentTarget.dataset.is_open_shop,
        total_num: e.currentTarget.dataset.total_num,
        goodsAttibutes: res.data.attributes,
        goodsId: e.currentTarget.dataset.goods_id
      })
    }
    })
    }
  },
  // 加入购物车
  addCar: function (e) {
    var _this = this;
    var goodsAttibutes = _this.data.goodsAttibutes;
    var chooseSize = _this.isChooseSize();
    var requestData;
    if (chooseSize) {
      if (goodsAttibutes.length == 0) {
        requestData = {
          version: app.setConfig.version,
          token: _this.data.token,
          type: app.setConfig.type,
          goods_id: _this.data.goodsId,
          num: _this.data.goodsNum
        };
      } else {
        var attr_thumb = _this.data.attributeImage;
        var subStr = new RegExp('http://source.xingliaoapp.com/');
        attr_thumb = attr_thumb.replace(subStr, "");
        requestData = {
          version: app.setConfig.version,
          token: _this.data.token,
          type: app.setConfig.type,
          goods_id: _this.data.goodsId,
          attr_values: _this.data.selectAttributs,
          attr_thumb: attr_thumb,
          attr_pric: _this.data.selectAttributeprice,
          attr_vip_pric: _this.data.attr_vip_pric,
          attr_fans_pric: _this.data.attr_fans_pric,
          attr_broker_pric: _this.data.attr_broker_pric,
          goods_attr_id: _this.data.goods_attr_id,
          num: _this.data.goodsNum,
          attributeImage: ''
        };
      }
      app.post(addCarUrl, requestData, function (res) {
        if (res.code != 2000) {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        } else {
          var car_shop_num = _this.data.car_shop_num + requestData.num;
          _this.setData({
            postage_money: res.data.postage_money,
            car_shop_num: car_shop_num++,
            ischooseSizeShow: 0,
            selectedAttributs: []
          })
          wx.showToast({
            title: '加入购物车成功',
            icon: 'none',
            duration: 2000
          })

        }
      });
    }
  },
  // 判断商品是否选择过属性
  isChooseSize: function () {
    var _this = this;
    var selectedAttributs = _this.data.selectedAttributs;
    var goodsAttibutes = _this.data.goodsAttibutes;
    if (goodsAttibutes.length != 0 && selectedAttributs.length == 0) {
      var ischooseSizeShow = _this.data.ischooseSizeShow;
      if (ischooseSizeShow == 1) {
        _this.refreshTips();
        wx.showToast({
          title: _this.data.selectTipsTitle + _this.data.selectTips,
          duration: 2000,
          icon: "none"
        })
      }
      _this.setData({
        ischooseSizeShow: 1
      })
      return false;
    } else {
      return true;
    }
  },
  /*检查用户地址*/
  checkAddress: function (buyType) {
    var _this = this;
    if (buyType == 0) {
      // 直接购买的商品
      if (this.data.address_default == 0) {
        // 没有默认地址跳转添加地址页
        wx.navigateTo({
          url: '/pages/addAddress/addAddress?goodsid=' + _this.data.goodsId + "&buyType=" + buyType + "&lucky_id=" + _this.data.lucky_id + "&draw_goods_id=" + _this.data.draw_goods_id,
        });
        return false;
      } else {
        //已经存在默认收货地址
        var cookie = wx.getStorageSync('cookie');

        return true;
      }
    } else {
      if (this.data.default_address == 0) {
        wx.navigateTo({
          url: '/pages/addAddress/addAddress?goodsid=' + _this.data.goodsId + "&buyType=" + buyType,
        });
        return false;
      } else {
        var requestData = {
          token: _this.data.token,
          goods_id: _this.data.goodsId,
          attr_values: _this.data.selectAttributs,
          lucky_id: _this.data.lucky_id,
          draw_goods_id: _this.data.draw_goods_id
        }
        app.post('/Goods/getGoods', requestData, function (res) {
          if (res.code == 2000) {
            wx.navigateTo({
              url: '/pages/orderDetail/orderDetail?orderId=' + res.last_id,
            })
          } else {
            new app.ToastPannel();
            _this.show(res.msg);
            wx.showToast({
              title: res.msg,
              icon: "none",
              duration: 2000
            });
          }
        });
      }
    }
  },
  /*刷新选择提示内容*/
  refreshTips: function () {
    var _this = this;
    var runSelectTips = _this.data.runSelectTips;
    var tips = "属性";
    runSelectTips.forEach((v, index) => {
      if (v.is_selected == 0) {
        tips = tips + " " + v.attr_name
      }
    });
    _this.setData({
      selectTips: tips,
      selectTipsTitle: "请选择"
    });
  },
  /*尝试更新提示状态*/
  uploadTips: function (attr_name, is_selected) {
    var _this = this;
    var runSelectTips = _this.data.runSelectTips;
    var newRunSelectTips = [];
    runSelectTips.forEach((v, index) => {
      if (is_selected == 1) {
        if (v.attr_name == attr_name) {
          newRunSelectTips.push({
            attr_name: v.attr_name,
            is_selected: 1
          })
        } else {
          newRunSelectTips.push({
            attr_name: v.attr_name,
            is_selected: v.is_selected
          })
        }
      } else {
        //取消选择
        if (v.attr_name == attr_name) {
          newRunSelectTips.push({
            attr_name: v.attr_name,
            is_selected: 0
          })
        } else {
          newRunSelectTips.push({
            attr_name: v.attr_name,
            is_selected: v.is_selected
          })
        }
      }
    });
    _this.setData({
      runSelectTips: newRunSelectTips
    })
    this.refreshTips();
  },
  // 获取店铺列表接口
  getGoodsList: function () {
    var _this = this;
    var requestData = {
      token: _this.data.token,
      version: app.setConfig.version,
      type: app.setConfig.type,
      shop_id: _this.data.shop_id,
      page: _this.data.page
    }
    app.post('/user/shopGoodsList', requestData, function (res) {
      if (res.code == 2000) {
        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh(); //停止下拉刷新
        for (var i = 0; i < res.data.list.length; i++) {
          res.data.list[i].img = _this.data.filesSourceUri + '/' + res.data.list[i].img;
        }
        var goodslist = _this.data.goodslist;
        goodslist = goodslist.concat(res.data.list);
       
        _this.setData({
          
          carousel_adv_info:res.data.carousel_adv_info,
          isLoading:0,
          page: res.data.page,
          goodslist: goodslist,
          shop_cart_is_show: res.data.shop_cart.is_show,
          car_shop_num: res.data.shop_cart.num,
          postage_money: res.data.shop_cart.postage_money,
          user_level:res.data.user.level,
          
        })
      }else{
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    });
  },
  // 获取公共配置文件
  getInitConfig: function () {
    var _this = this;
    var requestData = {
      token: _this.data.token,
      version: app.setConfig.version,
      type: app.setConfig.type
    }
    app.post('/GlobalConfig/getInitConfiga', requestData, function (res) {
      if (res.code == 2000) {
        wx.setStorageSync('configs', res.data.configs);
        console.log(res.data.configs)
        _this.setData({
          adv_info: res.data.adv_info,
          adv_solt: res.data.adv_solt,
          carousel_adv_info: res.data.carousel_adv_info,
          robot_list: res.data.robot_list,
          sourceUri: res.data.static_url,
          filesSourceUri: res.data.configs.static_url_http,
          localSourceUri: res.data.configs.static_url_http,
          configs: res.data.configs,
          bargain_buy_title: res.data.configs.bargain_buy_title
        })
      }else{
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    });
  },
  // 顶部机器人轮播结束执行的函数
  animationfinish: function () {
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    // console.log(options);
    var configs = wx.getStorageSync('configs');
    wx.getSystemInfo({
      success: function (res) {
        _this.setData({
          configs:configs,
          sourceUri: '',//静态资源地址
          filesSourceUri: configs.static_url_http,//普通资源文件地址
          localSourceUri: configs.static_url_http,//服务器目录存放地址
          postage_money: options.postage_money,
          shop_id: options.shop_id,
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        })
      }
    });
    _this.loop();
  },
  // 判断是否获取到token的值了
  loop: function () {
    var _this = this;
    var theToken = wx.getStorageSync("token");
    if (!theToken) {
      setTimeout(function () { _this.loop() }, 100);
    } else {
      _this.setData({
        token: theToken
      });
      _this.getGoodsList();
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this = this;
    _this.setData({
      isClick:1,
      selectedAttributs: [],
      ischooseSizeShow:0
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var _this = this;
    wx.showNavigationBarLoading() //在标题栏中显示加载
    _this.setData({
      goodslist: [],//存储首页书籍列表
      page: 1
    })
    _this.getGoodsList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var _this = this;
    // console.log('1');
    _this.getGoodsList();
  },
  /*角色权限控制*/
  getUserInfo: function (e) {
    var that = this;
    var infoUser = e.detail.userInfo;
    var res = e.detail;
    var url = app.setConfig.url + '/Login/doLogin';
    var data = {
      user_name: infoUser.nickName,
      nick_name: infoUser.nickName,
      head_img: infoUser.avatarUrl,
      sex: infoUser.gender,
      coutry: infoUser.country,
      city: infoUser.city,
      province: infoUser.province,
      code: app.globalData.code,
      encryptedData: res.encryptedData,
      signature: res.signature,
      ctype: app.setConfig.type,
      version: app.setConfig.version,
      iv: res.iv,
    }
    app.postLogin(url, data, function () {
      that.setData({
        displayAuthShow: "none"
      });
      that.loop();
    });
  },
  authRest: function () {
    this.setData({
      displayAuthShow: "none"
    });
    setTimeout(function () {
      wx.reLaunch({
        url: '/pages/home/home',
      })
    }, 1000)
  },
  // 点击顶部导航跳转广告详情
  jumpDetail: function (e) {
    var advType = e.currentTarget.dataset.type_detail;
    console.log(advType);
    if (advType.adv_type == 3) {
      wx.navigateToMiniProgram({
        appId: advType.app_id.toString(),
        path: advType.page.toString(),
        extraData: advType.sence.toString(),
        complete(res) {
          console.log(res)
        }
      });
    } else if (advType.adv_type == 4) {
      wx.navigateTo({
        url: '/' + advType.adv_uri,
      })
    }
  },
  /*红包广告跳转事件*/
  goAdvEvent: function (e) {
    var _this = this;
    var adv_solt = _this.data.adv_solt;
    if (_this.data.adv_solt) {
      //广告数据不为空
      if (adv_solt.adv_type == 1) {
        //图片类
        wx.navigateTo({
          url: '/pages/rule/rule?url=' + adv_solt.adv_info_url
        });
      } else if (adv_solt.adv_type == 3) {
        //小程序类
        wx.navigateToMiniProgram({
          appId: adv_solt.app_id.toString(),
          path: adv_solt.page.toString(),
          extraData: adv_solt.sence.toString(),
          complete(res) {
            console.log(res)
          }
        });
      } else {
        //内部网页类型
        wx.navigateTo({
          url: "/" + adv_solt.adv_uri,
        })
      }
    }
  },
  /*选择属性值事件*/
  selectAttributeEvent: function (e) {
    var _this = this;
    if (e.type == "tap") {
      var attr_id = e.currentTarget.dataset.attr_id;
      var goods_attr_id = e.currentTarget.dataset.goods_attr_id;
      var goodsAttibutes = _this.data.goodsAttibutes;
      var attrThumb = e.currentTarget.dataset.thumb;
      var attrPric = e.currentTarget.dataset.attr_pric;//属性优品价格
      var attr_original_pric = e.currentTarget.dataset.attr_original_pric;//商品原价
      var attr_fans_pric = e.currentTarget.dataset.attr_fans_pric;//属性粉丝价
      var attr_vip_pric = e.currentTarget.dataset.attr_vip_pric;//属性会员价
      var attr_broker_pric = e.currentTarget.dataset.attr_broker_pric;//属性用金价
      var attr_num = e.currentTarget.dataset.attr_num;//属性个数
      // console.log(attr_num);
      var goodsNum = _this.data.goodsNum;
      if (goodsNum > attr_num) {
        wx.showToast({
          title: '购买数量不得大于库存',
          icon: 'none',
          duration: 2000,
          success: function () {
            _this.setData({
              goods_attr_id: goods_attr_id,
              goodsNum: attr_num,
              total_num: attr_num
            })
          }
        })

      } else {
        _this.setData({
          goods_attr_id: goods_attr_id,
          total_num: attr_num
        })
      }
      var newGoodsAttributes = [];
      goodsAttibutes.forEach((currentValue, index, goodsAttibutes) => {
        if (attr_id == currentValue.attr_id) {
          var newAttrExt = [];
          currentValue.attr_ext.forEach((currentExtValue, extIndex) => {
            if (currentExtValue.goods_attr_id == goods_attr_id) {
              if (currentExtValue.is_selected == 0) {
                //获取已选择的属性分类
                var selectedAttributs = _this.data.selectedAttributs;
                if (!selectedAttributs.in_array(currentExtValue.attr_name)) {
                  selectedAttributs.push(currentExtValue.attr_name);
                  _this.setData({
                    selectedAttributs: selectedAttributs
                  });
                }
                currentExtValue.is_selected = 1;
                _this.uploadTips(currentExtValue.attr_name, 1);
                if (attrPric > 0.00) {
                  if (_this.data.goods_level == 2 || _this.data.goods_level == 3) {
                    attrPric = e.currentTarget.dataset.attr_pric;
                  }
                  _this.setData({
                    selectAttributeprice: attrPric,
                  });
                }
                if (attrThumb) {
                  _this.setData({
                    attributeImage: _this.data.filesSourceUri + '/' + attrThumb
                  });
                }
              } else {
                var selectedAttributs = _this.data.selectedAttributs;
                if (selectedAttributs.in_array(currentExtValue.attr_name)) {
                  selectedAttributs.remove(currentExtValue.attr_name);
                  _this.setData({
                    selectedAttributs: selectedAttributs
                  });
                }
                currentExtValue.is_selected = 0;
                _this.uploadTips(currentExtValue.attr_name, 0);
                var selectAttributeprice = _this.data.buy_price;
                if (_this.data.goods_level == 2 || _this.data.goods_level == 3) {
                  selectAttributeprice = _this.data.goods.buy_price;
                }
                _this.setData({
                  selectAttributeprice: selectAttributeprice,
                  attributeImage: _this.data.attributeImage
                });
              }
            } else {
              currentExtValue.is_selected = 0;
            }
            newAttrExt.push(currentExtValue);
          });
          currentValue.attr_ext = newAttrExt;
          newGoodsAttributes.push(currentValue);
        } else {
          newGoodsAttributes.push(currentValue);
        }
      });

      _this.setData({
        attr_original_pric: attr_original_pric,
        goodsAttibutes: newGoodsAttributes,
        attr_fans_pric: attr_fans_pric,
        attr_vip_pric: attr_vip_pric,
        attr_broker_pric: attr_broker_pric
      });
    }
    //更新选择的属性值
    _this.uploadSelectAttributes();
    if (_this.data.attributeImage == '') {
      _this.setData({
        attributeImage: _this.data.filesSourceUri + '/' + _this.data.carousel_img[0]
      })

    }
  },
  //检查属性选择情况
  checkGoodsAttribute: function () {
    var _this = this;
    //获取必选项的数据
    var selectedMust = [];
    _this.data.goodsAttibutes.forEach((attributes, index) => {
      if (attributes.is_required == 1) {
        if (!selectedMust.in_array(attributes.attr_name)) {
          selectedMust.push(attributes.attr_name);
        }
      }
    });
    return selectedMust.difference(_this.data.selectedAttributs);
  }
})